package com.xd.common.ascept;


import com.google.gson.*;
import com.xd.common.exception.XkdException;
import com.xd.common.util.XkdUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;


@Slf4j
@Aspect
@Component
@Order(1)
public class CheckH5UrlAspect {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private static final String H5_LINK = "H5_LINK:";//H5链接


    @Pointcut("@annotation(CheckH5Url)")
    public void checkH5UrlAroud() {

    }


    //验证生成的链接是否过期
    @Around(value = "checkH5UrlAroud()")
    public Object checkH5(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        Object o = redisTemplate.opsForValue().get(H5_LINK + token);
        if (o == null) {
            throw new XkdException("链接已过期");
        }
        return joinPoint.proceed();
    }


}
